<?php
/**
 * 通知的处理
 */

$_POST = $_DPOST;
if(!$_POST) exit('fail');
$bank = 'alipay';
$PAY = PCache::read('pay.php');
if(!$PAY[$bank]['enable']) exit('fail');
if(!$PAY[$bank]['partnerid']) exit('fail');
if(!$PAY[$bank]['keycode']) exit('fail');
require DT_INC.'/module.func.php';
$receive_url = '';
function log_result($word) {
	log_write($word, 'nalipay');
}
require DT_ROOT.'/api/pay/alipay/notify.class.php';
require DT_ROOT.'/api/pay/alipay/config.inc.php';
$alipay = new alipay_notify($partner,$security_code,$sign_type,$_input_charset,$transport);
$verify_result = $alipay->notify_verify();
if($verify_result) {
	$do = new OwnCardModule();
	$owncardAction = Db('owncard');
	$buyinfo = $owncardAction->getByitem($out_trade_no);
	if($buyinfo) {
		if($buyinfo['status'] == 1) {
			$charge_orderid = $buyinfo['itemid'];
			$charge_money = $buyinfo['money'];
			$charge_amount = $buyinfo['money'];
			$editor = 'N'.$bank;
			if($total_fee == $charge_money) {
                if($trade_status =='TRADE_SUCCESS'||$trade_status=='TRADE_FINISHED'){//检测返回的支付宝付款状态，如果是付款成功，则改变状态
	                $buyinfo['status']  = 2;
	                $buyinfo['buytime'] = $DT_TIME;
	                $owncardAction->updateBuycard($buyinfo);
	                if($buyinfo['xdmoney']){
		                money_add($buyinfo['username'], -$buyinfo['xdmoney']);
		                money_record($buyinfo['username'], -$buyinfo['xdmoney'], $PAY[$bank]['name'], 'system', '车主金卡购买', "订单号：$charge_orderid;");
	                }
	                send_message($buyinfo['username'],"您的车主金卡订单已确认收到！","您已成功订购车主金卡(".$buyinfo['buynum'].")张，我们的客服将尽快与您联系发货！");
                    exit('success');
                }  else {
                    exit('fail');
                }
			} else {
				$note = '充值金额不匹配S:'.$charge_money.'R:'.$total_fee;
				$buyinfo['status']  = 4;
				$owncardAction->updateBuycard($buyinfo);
				log_result($note);
				exit('fail');
			}
		} else if($r['status'] == 1) {
			exit('fail');
		} else if($r['status'] == 2) {
			exit('fail');
		} else {
			exit('success');
		}
	} else {
		log_result('通知订单号不存在R:'.$out_trade_no);
		exit('fail');
	}
}
exit('fail');
?>